package com.executor.future;

import com.sunfeng.TimeUtils;

import java.util.ArrayList;
import java.util.concurrent.*;

/**
 * @Author : 孙峰
 * @Description: 当有任意一直执行完成则返回   其他执行的线程则会被打断
 * @Date : 2022/10/6  15:34
 */
public class Demo522InvokeAny {
    private static ExecutorService executorService = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ArrayList<Callable<Integer>> callables = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            callables.add(() -> {
                int nextInt = ThreadLocalRandom.current().nextInt(30);
                System.out.println(nextInt);
                TimeUtils.sleep(nextInt);
                System.out.println("Task " + nextInt + "完成 线程是" + Thread.currentThread());
                return nextInt;
            });
        }

        Integer integer = executorService.invokeAny(callables);

        System.out.println("结果为：" + integer);
       // executorService.shutdown();
    }

}
